﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using SystemAnalizer.Common;
using System.Windows.Input;
using SystemAnalizer.Utils;
using SystemAnalizer.Algorithms;
using SystemAnalizer.Models;

namespace SystemAnalizer.ViewModel
{
    public class MainWindowViewModel
    {
        private SystemModel system;
        private List<IdStr> PerSystemSelfPolyAlgorithms = new List<IdStr>();
        private List<IdStr> PerSystemEigValAlgorithms = new List<IdStr>();
        private List<IdStr> PerSystemSelfPolyModif = new List<IdStr>();
        private List<IdStr> NonPerSystemSelfPolyModif = new List<IdStr>();
        public MainWindowViewModel()
        {
            CommandsLoader();
            InitAllCombo();
        }

        private void InitAllCombo()
        {
            this.PerSystemSelfPolyAlgorithms.Add(new IdStr(AlgorithmTypes.FADDEEV, "Faddeev"));
            this.PerSystemSelfPolyModif.Add(new IdStr(AlgorithmTypes.DT, "DT"));
            this.PerSystemSelfPolyModif.Add(new IdStr(AlgorithmTypes.DP, "DP"));
            this.PerSystemEigValAlgorithms.Add(new IdStr(AlgorithmTypes.LR, "LR"));
            this.NonPerSystemSelfPolyModif.Add(new IdStr(AlgorithmTypes.SAMUELSON, "Samuelson"));
        }

        public List<IdStr> GetPerSystemSelfPolyAlgorithms
        {
            get { return this.PerSystemSelfPolyAlgorithms; }
        }
        public List<IdStr> GetPerSystemEigValAlgorithms
        {
            get { return this.PerSystemEigValAlgorithms; }
        }
        public List<IdStr> GetPerSystemSelfPolyModif
        {
            get { return this.PerSystemSelfPolyModif; }
        }
        public List<IdStr> GetNonPerSystemSelfPolyModif
        {
            get { return this.NonPerSystemSelfPolyModif; }
        } 

        private void CommandsLoader()
        {
            CreateNewPerSystemCommand = new ViewModelCommand(CreateNewPerSystem, (o) => { return true; });
            CreateNewNonPerSystemCommand = new ViewModelCommand(CreateNewNonPerSystem, (o) => { return true; });
            CloseMainWindowCommand = new ViewModelCommand(CloseMainWindow, (o) => { return true; });
            CalculatePeriodicPolynomCoefFaddeevCommand = new ViewModelCommand(CalculatePeriodicPolynomCoefFaddeev, (o) => { return true; });
            CalculatePeriodicEigValueLRCommand = new ViewModelCommand(CalculatePeriodicEigValueLR, (o) => { return true; });
        }

        // CalculatePeriodicEigValueLR
        private void CalculatePeriodicEigValueLR(object param)
        {
            MainWindow mwind = param as MainWindow;
            LR lr = new LR(this.system.System_size, this.system.number_discretes, this.system.System_Discretes);
            ViewHelper.FaddeevTraceAdder(mwind.SelfCoefFaddeev, lr.calculate_eigvalue(), true, this.system.System_size, this.system.number_discretes);
        }

        // CalculatePeriodicPolynomCoefFaddeev
        private void CalculatePeriodicPolynomCoefFaddeev(object param)
        {
            MainWindow mwind = param as MainWindow;
            Faddeev fd = new Faddeev(this.system.System_size, this.system.number_discretes, this.system.System_Discretes);
            ViewHelper.FaddeevTraceAdder(mwind.SelfCoefFaddeev, fd.calculate_coef(), true, this.system.System_size,this.system.number_discretes);
        }

        // CloseMainWindow
        private void CloseMainWindow(object param)
        {
            ((Window)param).Close(); 
        }

        // CreateNewSystem
        private void CreateNewPerSystem(object param)
        {
            CreateSystem crsys = new CreateSystem(((MainWindow)param).ProgrammSetting,true);
            crsys.ShowDialog();
            if ((bool)crsys.DialogResult)
            {
                crsys.Createsystem_viewmodel.InitContainerwithDiscretes(((MainWindow)param).PeriodicSystemdiscretes);
                crsys.Createsystem_viewmodel.InitContainerwithSystemInfo(((MainWindow)param).CreatePerSysInputVal);
                this.system = crsys.Createsystem_viewmodel.GetSystem;
            }

        }

        private void CreateNewNonPerSystem(object param)
        {
            CreateSystem crsys = new CreateSystem(((MainWindow)param).ProgrammSetting,false);
            crsys.ShowDialog();
            if ((bool)crsys.DialogResult)
            {
                crsys.Createsystem_viewmodel.InitContainerwithDiscretes(((MainWindow)param).NonPeriodicSystemdiscretes);
             //   crsys.Createsystem_viewmodel.InitContainerwithSystemInfo(((MainWindow)param).CreatePerSysInputVal);
                this.system = crsys.Createsystem_viewmodel.GetSystem;
            }
        }
 

        #region commands 
        public ICommand CloseMainWindowCommand { get; set; }
        public ICommand CreateNewPerSystemCommand { get; set; }
        public ICommand CreateNewNonPerSystemCommand { get; set; }
        public ICommand CalculatePeriodicPolynomCoefFaddeevCommand { get; set; }
        public ICommand CalculatePeriodicEigValueLRCommand { get; set; }
    
        #endregion   // commands
    }

    
}
